import bb.cascades 1.0
import bb.system 1.0
import bb.cascades.pickers 1.0

NavigationPane {
    id: nav
    backButtonsVisible: true
    Menu.definition: MenuDefinition {
        helpAction: HelpActionItem {

            onTriggered: {
                infoSheet.open()
            }
            attachedObjects: [
                Sheet {
                    id: infoSheet
                    content: Page {
                        Label {
                            multiline: true
                            text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.Nulla et arcu tellus, ac feugiat sem. Praesent vel ligula elit."
                        }
                        actions: [
                            ActionItem {
                                title: "Ok"
                                ActionBar.placement: ActionBarPlacement.OnBar
                                onTriggered: {
                                    infoSheet.close();
                                }
                            }

                        ]
                    }
                }
            ]
        }
        settingsAction: SettingsActionItem {
            onTriggered: {
                var page = settingsPageDefinition.createObject();
                nav.push(page);
            }
        }
    }
    Page {
        id: stampListPage

        Container {

            ListView {
                id: listView
                objectName: "listView"

                dataModel: _db.categoryModel
                function getImporter(){
                    return _importer;
                }

                function itemType(data, indexPath) {
                    return (indexPath.length == 1 ? 'category' : 'set');
                }

                listItemComponents: [
                    ListItemComponent {
                    	type: "category"
                        Header {
                            id: categoryId
                            title: ListItemData.name
                            subtitle: ListItemData.desc
                            attachedObjects: [
                                FilePicker {
                                    id: filePicker
                                    type: FileType.Other
                                    title: "Question Set Import"
                                    onFileSelected: {
                                        console.log("File selected: " + selectedFiles)
                                        console.log("Category: " + categoryId.ListItem.data.id)
                                        categoryId.ListItem.view.getImporter().Import(selectedFiles, categoryId.ListItem.data.id)
                                    }
                                }
                            ]
                            contextActions: [
                                ActionSet {
                                    ActionItem {
                                        title: qsTr("Import set")

                                        onTriggered: {
                                            filePicker.open();
                                        }
                                    }

                                }
                                ]
                        }
                    },
                    ListItemComponent {
                        type: "set"

                        StandardListItem {
                            id: itemId
                            title: ListItemData.infoText
                            description: ListItemData.desc

                            contextActions: [
                                ActionSet {
                                    ActionItem {
                                        title: qsTr("Open")

                                        onTriggered: {
                                            console.log("Open clicked" + itemId.title + " " + description)
                                            itemId.ListItem.view.openItem(itemId.ListItem.indexPath)
                                        }
                                    }

                                    ActionItem {
                                        title: qsTr("Edit")

                                        onTriggered: {
                                            console.log("Edit clicked" + itemId.title + " " + description)
                                        }

                                    }
                                    ActionItem {
                                        title: qsTr("Delete")
                                        imageSource: "icons/delete.png"

                                        onTriggered: {
                                            console.log("Delete clicked" + itemId.title + " " + description)
                                        }

                                    }
                                }
                            ]

                        }

                    }
                ]
                function openItem(itemIndex) {
                    if (itemIndex.length > 1) {
                        var chosenItem = dataModel.data(itemIndex);
                        var contentpage = contentPageDefinition.createObject();
                        contentpage.item = chosenItem;
                        nav.push(contentpage);
                    }

                }
                
                onTriggered: {
                	openItem(indexPath)
                }
            }
        }

        actions: [
            ActionItem {
                title: "Add item"
                ActionBar.placement: ActionBarPlacement.InOverflow
                imageSource: "icons/add.png"
                onTriggered: {
                    addSheet.open()
                    console.log("Add item clicked")
                }
            },
            ActionItem {
                title: "Clear items"
                ActionBar.placement: ActionBarPlacement.InOverflow
                imageSource: "icons/clear.png"
                onTriggered: {
                    console.log("Clear items clicked")
                }
            }
        ]
    }

    attachedObjects: [
        ComponentDefinition {
            id: contentPageDefinition
            source: "content.qml"
        },
        ComponentDefinition {
            id: settingsPageDefinition
            source: "settings.qml"
        },
        Sheet {
            id: addSheet
            content: Page {
                Container {
                    layout: StackLayout {}
                    Header {
                        title: "New Category"
                    }
                    Label {
                        text: "Name:"
                    }
                    TextField {
                    	id: name
                    }
                    Label {
                        text: "Description:"
                    }
                    TextField {
                        id: desc
                    }

                }
                

                actions: [
                    ActionItem {
                        title: "Cancel"
                        ActionBar.placement: ActionBarPlacement.OnBar
                        imageSource: "asset:///icons/clear.png"
                        onTriggered: {
                            addSheet.close();
                        }
                    },
                    ActionItem {
                        title: "Save"
                        enabled: name.text != ''
                        imageSource: "asset:///icons/add.png"
                        ActionBar.placement: ActionBarPlacement.OnBar
                        onTriggered: {
                            _db.addCategory(name.text, desc.text);
                            addSheet.close();
                        }
                    }

                ]
            }
        }
    ]
}
